/*
 *  Copyright (C) 2010 MADSA.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author: Victor Mayoral Vilches
 *          Jorge Pintado de Santiago
 */

package com.madsa.tuPeli;

import com.madsa.tuPeli.option.OptionsMenu;
import com.sisof.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class MenuPrincipal extends Activity implements OnClickListener {

	private static final int SELECCION_TUPELI = 0;
	private static final int TUPELI = Menu.FIRST;
    private static final int CARTELERA = Menu.FIRST+1;
    private static final int OPCIONES = Menu.FIRST+2;
    private static final int ACERCADE = Menu.FIRST+3;
    private static final int SALIR = Menu.FIRST+4;

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case SELECCION_TUPELI:	            
			LayoutInflater factory = LayoutInflater.from(this);
			final View layout = factory.inflate(R.layout.alert_dialog_text_entry, null);
			final TextView text = (TextView) layout.findViewById(R.id.tuPeliSelection);
			
			return new AlertDialog.Builder(MenuPrincipal.this)
					.setIcon(17301571)
					.setTitle("Introduzca el nombre de la película:")
					.setView(layout)
					.setPositiveButton("ok", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {	                    	
							Intent i = new Intent(MenuPrincipal.this, TuPeliActivity.class);
							i.putExtra("pelicula",text.getText());
							startActivity(i);
						}	
					})
					.setNegativeButton("cancelar",null)
					.create();
	        }
	        return null;
	    }	  

	
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.main);
              
        //Only vertical use mode
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                      
        //Get layout widgets (buttons)
        View salirBoton = findViewById(R.id.salir);
        View infoBoton = findViewById(R.id.info);
        View carteleraBoton = findViewById(R.id.cartelera);
        View muestraBoton = findViewById(R.id.tuPeli);
        View opcionBoton = findViewById(R.id.configuracion);
        
        salirBoton.setOnClickListener(this);
        infoBoton.setOnClickListener(this);
        carteleraBoton.setOnClickListener(this);
        muestraBoton.setOnClickListener(this);
        opcionBoton.setOnClickListener(this);
       
    }


	public void onClick(View v) {
		switch (v.getId()) {
		  case R.id.salir:
			  exitAppFunction();
		     break;

		  case R.id.info:
			  dialogoInformacion();
			  break;

		  case R.id.cartelera:
			  Intent i = new Intent(MenuPrincipal.this, Catelera.class);
			  startActivity(i);
			  break;

		  case R.id.tuPeli:
			  showDialog(SELECCION_TUPELI);	  
			  break;

		  case R.id.configuracion:
			  Intent acOption = new Intent(this, OptionsMenu.class);
			  startActivity(acOption);
			  break;
		}
	}

	private void exitAppFunction() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Seguro que quiere salir de la aplicación?")
				.setCancelable(false)
				.setPositiveButton("Si", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						MenuPrincipal.this.finish();		                  
					}
				})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();		           
					}
				});
		AlertDialog alert = builder.create(); 	
		alert.show();
	}


	private void dialogoInformacion(){
		
		Dialog d = new Dialog(MenuPrincipal.this);
		Window window = d.getWindow();
		window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		
		d.setTitle("Información");
		d.setContentView(R.layout.information_dialog);
		TextView text = (TextView) d.findViewById(R.id.information_dialog);
		
		text.setText(" MADSA presents a new application that will change the world!\n\n");
		text.append(" Developers:\n");
		text.append(" Jorge Pintado de Santiago\n");
		text.append(" Victor Mayoral Vilches\n");
		text.append(" Adrián García López\n");
		text.append(" Javier Rodríguez Robles\n");
		text.append(" Luis Alonso Blazquez\n");
		text.append(" Fernando José Iglesias\n");
		d.show();
	}
		
	public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(Menu.NONE, TUPELI, 0, R.string.tupeli_textmenu);
        menu.add(Menu.NONE, CARTELERA, 1, R.string.cartelera);
        menu.add(Menu.NONE, OPCIONES, 2, R.string.option);
        menu.add(Menu.NONE, ACERCADE, 3, R.string.acerca);
        menu.add(Menu.NONE, SALIR, 4, R.string.exit);
        return true;
    }

    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        super.onMenuItemSelected(featureId, item);
              switch(item.getItemId()) {
              case TUPELI:
    			  showDialog(SELECCION_TUPELI);
                  break;
              case CARTELERA:
    			  Intent i = new Intent(MenuPrincipal.this, Catelera.class);
    			  startActivity(i);
            	  break;
              case OPCIONES:
    			  Intent acOption = new Intent(this, OptionsMenu.class);
    			  startActivity(acOption);
            	  break;
              case ACERCADE:
            	  dialogoInformacion();
            	  break;
              case SALIR:
            	  exitAppFunction();
            	  break;
              }
              
              return true;
       }

}